package com.mes.controller;

import com.mes.common.FormatUtilities;
import com.mes.entity.User;
import com.yy.base.page.Message;
import com.yy.system.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/wx")
public class ADUserUpdateController {

    @Autowired
    private LoginService service;

    @ResponseBody
    @RequestMapping(value="/checkUser.html")
    public Message checkUser(HttpServletRequest request) {
        Message message = new Message();
        try {
            String userCode = request.getParameter("userName");
            String password = request.getParameter("passwd");

            if (userCode != null) {
                User user = service.login(userCode);
                if (user != null) {
                    String pwd = FormatUtilities.sha1Base64(password);
                    if (user.getPassword().equals(pwd)) {
                        String openId = request.getParameter("openId");
                        String flag = request.getParameter("flag");
                        if(flag.equalsIgnoreCase("UPDATE")){
                            int count = service.updateUser(user.getId(),openId);
                            if(count != 1){
                                message.setErrorMessage("绑定错误，请仔细用户名和密码是否输入正确。");
                                return message;
                            }
                            message.setOkMessage("绑定成功");
                            return message;
                        }
                    } else {
                        message.setErrorMessage("用户名或密码错误!");
                    }
                } else {
                    message.setErrorMessage("用户名或密码错误!");
                }
            } else {
                message.setErrorMessage("登录方式异常!");
            }
        } catch (Exception e) {
            message.setErrorMessage("绑定失败!");
        }
        return message;
    }



}
